﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AplicacaoReserva.ReservasForms
{
    public partial class ListarReservasForm : Form
    {
        public int ReservaID;
        public int ClienteID;
        public int AgenciaID;

        public ListarReservasForm()
        {
            InitializeComponent();
        }

        private void ListarReservasForm_Load(object sender, EventArgs e)
        {
            try
            {
                ReservaServiceReference.WCFReservaClient wcfReservas = new ReservaServiceReference.WCFReservaClient();
                if (ClienteID != 0)
                    dgv_ListaReservas.DataSource = wcfReservas.getByCliente(ClienteID);
                else if (AgenciaID != 0)
                    dgv_ListaReservas.DataSource = wcfReservas.getByAgencia(AgenciaID);
                else
                {
                    MessageBox.Show("Não tem reservas disponíveis", "Informação!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Occoreu um erro:\n" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            EditarReservaForm editReserva = new EditarReservaForm();
            editReserva.idReserva = ReservaID;
            editReserva.MdiParent = MdiParent;
            editReserva.Show();
        }

        private void dgv_ListaReservas_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                ReservaID = int.Parse(dgv_ListaReservas.Rows[e.ColumnIndex].Cells[0].Value.ToString());
            }
            catch(Exception) {
                MessageBox.Show("Occoreu um erro:\n", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            btnEditar.Enabled = true;
            btnRemover.Enabled = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRemover_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Tens a certeza?", "Remover Reserva", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    ReservaServiceReference.WCFReservaClient reservas = new ReservaServiceReference.WCFReservaClient();

                    reservas.delete(ReservaID);

                    MessageBox.Show("Removido com sucesso!", "Remover Reserva", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Occoreu um erro:\n" + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
